Visualization
In my earlier analysis, I delved into the Sales_Export_2019_2020
dataset, conducting data exploration and cleaning. In this phase, I will
generate visualizations aimed at gaining insights into various
aspects:
I intend to identify the countries with the highest cost and order values, shedding light on their respective standings.
Additionally, I plan to investigate the discrepancies in cost and order values across different categories within each country, providing a comprehensive view of category-specific performance.
Furthermore, I aim to examine the cumulative profit earned within each country and category, providing a holistic perspective on profitability.
By creating these visualizations, I hope to derive meaningful patterns and trends that will contribute to a more profound understanding of the dataset and its underlying dynamics.
Sales By Country and Category
#Smoothed scatter plot of order values in Euros against countries using the #Sales_Export_2019_2020 dataset.
#
orderval <- ggplot(data = Sales_Export_2019_2020, aes(x = country, y = order_value_EUR, color = category, fill = category)) + geom_smooth(method = 'loess', formula = 'y ~ x', aes(group = category)) + scale_y_continuous(labels = scales::comma) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Order Values in Each Country (2019-2020))", x = "Country", y = "Order Value (EURO)")
#converting ggplot graph into a interactive Plotly graph, with a customized tooltip.
p <- ggplotly(orderval, tooltip = c("x", "y", "color"))
#Displaying Plotly graph.
p
I utilize the ggplot function with our Sales_Export_2019_2020 data to create a visualization. To establish visual characteristics, I make use of the aes function, with the x-axis displaying the country variable and the y-axis representing “Order_value_EUR.” Furthermore, I associate the “category” variable with color and fill aesthetics to differentiate between visual categories.
I create a smoothed line on the plot using the geom_smooth function with the “loess” method. The group aesthetic is set to “category” to ensure each category has its own smoothed line, and the smoothing equation is outlined in the formula parameter.
To enhance the legibility of x-axis text labels, I use the theme function to adjust the angle to 45 degrees and align the labels to the right using the hjust = 1 setting. I also format the y-axis labels using the scale_y_continuous function with the scales::comma function to add commas for better readability.
To add a title and axis labels, I use the labs function. Lastly, I convert the ggplot visualization (orderval) into an interactive Plotly graph with the ggplotly function, and customize the tooltip parameter to display the relevant information when hovering over data points.
The resulting interactive Plotly graph (p) provides an insightful visualization of order values across various countries, enriched with interactivity and tailor-made tooltips.
#Grouped column chart to visualize the total sales of each country's exports by category
#using the CountryTotalSaleExport dataset.
#Each country is represented on the x-axis, and the total sale values are shown as grouped #columns with different colors representing categories.
TCCSales <- ggplot(data = CountryTotalSaleExport, aes(x = country, y = saleCategoryTotal, fill = category)) + geom_col() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Total Sales of Each Countries Exports by Category (2019-2020))", x = "Country", y = "Cost Total") + scale_y_continuous(labels = scales::comma)
#Converting ggplot graph into an interactive plotly graph.
tcc <- ggplotly(TCCSales)
#Displaying Plotly graph.
tcc
This code generates an interactive plotly graph based on a ggplot2 visualization, displaying the total sales of countries’ exports by category. The ggplot function creates a bar plot (geom_col) using the dataset CountryTotalSaleExport with defined aesthetics (aes) for the x-axis representing the “country” variable and the y-axis representing “saleCategoryTotal.” The “category” variable determines the fill color of the bars. Customizations are applied to the plot, including angled and right-aligned x-axis text labels, a title, specified labels for the x and y axes, and formatted y-axis labels with commas for thousands. The ggplot object (TCCSales) is converted into an interactive plotly graph using the ggplotly function, which allows for interactive features like hovering, zooming, and panning. The result is an easy-to-understand visualization of sales data using both ggplot2 and plotly in R.
Cost Per Category for Each Country
#Scatter plot with connected lines to visualize the total cost of each country's exports by #category using the CountryTotalcostExport dataset.
#Each country is represented on the x-axis, and the total cost values are shown as scatter #points with lines connecting points of the same category.
TCCCost <- ggplot(data = CountryTotalcostExport, aes(x = country, y = costCategoryTotal, colour = category)) + geom_point() + geom_line(aes(group = category)) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Total Cost of Each Countries Exports by Category (2019-2020))", x = "Country", y = "Cost Total (Euros)") + scale_y_continuous(labels = scales::comma)
#Converting ggplot graph into interactive Plotly graph.
TCCC <- ggplotly(TCCCost)
#Displaying Plotly Graph.
TCCC
This code generates an interactive graph using Plotly based on a ggplot2 visualization. The scatter plot connects the points with lines, created by the ggplot function. The data used is the CountryTotalcostExport dataset, with the x-axis representing the “country” variable and the y-axis representing “costCategoryTotal”, while the “category” variable determines the color of the points and lines.
To customize the appearance of the plot, the x-axis text labels are angled at 45 degrees and aligned to the right, a title is added, and labels for the x and y axes are specified. The y-axis labels are also formatted with commas for thousands.
The ggplot object (TCCCost) is then forcefully converted into an interactive Plotly graph using the ggplotly function. This conversion retains interactivity features such as tooltips, zooming, and panning. Finally, the interactive Plotly graph (TCCC) is assertively displayed.
In summary, this code allows for the visualization of the total cost of countries’ exports by category using both ggplot2 and Plotly in R. The scatter points are connected by lines for each category, allowing for the observation of trends over the x-axis (countries) for different cost categories.
Gross Profit for Each Country
#Scatter plot to visualize the cross profit per country using the CountryProfit dataset.
#Each country is represented on the x-axis, and the total gross profit values are shown as scatter points.
#The x-axis labels are reordered based on the total gross profit values, and the plot #includes axis labels, title, and formatted y-axis labels.
CGProfit <- ggplot(data = CountryProfit, aes(x = reorder(country, GrossProfit), y = GrossProfit)) + geom_point() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Gross Profit Per Country (2019-2020))", x = "Country", y = "Total Gross Profit (Euros)") + scale_y_continuous(labels = scales::comma)
#Converting ggplot graph into interactive Plotly graph, with customized tooltip.
CG <- ggplotly(CGProfit, tooltip = c("x", "y"))
#Displaying Plotly Graph
CG
The generation of an interactive graph using Plotly based on a ggplot2 visualization is made easy with this program. The scatter plot is expertly created using the ggplot function, skillfully connecting the points using lines. The dataset used is the CountryTotalcostExport, where the x-axis represents “country” and the y-axis represents “costCategoryTotal,” while the “category” variable determines the color of the points and lines.
To improve the plot’s appearance, the x-axis text labels are angled at 45 degrees and aligned to the right, a title is added, and precise labels for the x and y axes are specified. The y-axis labels are also meticulously formatted with commas for thousands.
The ggplot object (TCCCost) is then seamlessly transformed into an interactive Plotly graph using the ggplotly function, flawlessly preserving interactive features like tooltips, zooming, and panning. Finally, the interactive Plotly graph (TCCC) is effortlessly displayed.
In conclusion, this program allows for the visualization of the total cost of countries’ exports by category with both ggplot2 and Plotly in R. The scatter points are cleverly linked by lines for each category, enabling the observation of trends over the x-axis (countries) for different cost categories. With this program, you can confidently create impressive interactive graphs with ease.
Gross Profit based on Category
#Heatmap to visualize gross profit by category using the CategoryProfit dataset.
#Each category is represented on the x-axis, and the gross profit values are shown as #colored tiles.
CProfit <- ggplot(data = CategoryProfit, aes(x = category, y = GrossProfit, fill = category)) + geom_tile(aes()) + scale_y_continuous(labels = scales::comma) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Gross Profit by Category (2019 - 2020", x = "Category", y = "Gross Profit (Euros)")
#Convert ggplot graph into interactive Plotly graph, with customized tooltip.
CP <- ggplotly(CProfit, tooltip = c("x", "y"))
#Display Plotly graph.
CP
This code generates an interactive Plotly graph based on a ggplot2 visualization to create a tiled heatmap using data from the CategoryProfit dataset. The aesthetics are set up with the x-axis representing the “category” variable, the y-axis representing “GrossProfit,” and the fill color determined by the “category” variable. Customizations are applied to the appearance of the plot, including formatting the y-axis labels with commas for thousands, angling the x-axis text labels at 45 degrees and aligning them to the right, assigning a title to the plot, and specifying labels for the x and y axes.
The ggplot object (CProfit) is then converted into an interactive Plotly graph using the ggplotly function, with the tooltip parameter set up to display the “x” and “y” values when hovering over the tiles.
In short, this code allows for the visualization of Gross Profit by category for the years 2019-2020 using both ggplot2 and Plotly in R. The heatmap tiles represent different categories, and the interactivity enables viewing of specific values by hovering over the tiles.
#Load Shiny library.
library(shiny)
#Define UI.
ui <- fluidPage(
titlePanel("Interactive Pie Chart"),
sidebarLayout(
sidebarPanel(
selectInput("country", "Select Country:", choices = unique(CoProfit$country))
),
mainPanel(
plotlyOutput("pieChart")
)
)
)
#Define server.
server <- function(input, output) {
output$pieChart <- renderPlotly({
country_data <- CoProfit %>%
filter(country == input$country) #Filter data where country is the same.
#Copy category in country_data into labels.
labels <- country_data$category
values <- country_data$GProfit #Copy Gross Profit in country_data into values.
#Create interactive graph using Plotly.
pie_chart <- plot_ly(type = 'pie', labels = labels, values = values,
textinfo = 'label+percent',
insidetextorientation = 'radial')
# Customize the Plotly Graph
pie_chart <- pie_chart %>%
layout(title = paste("Total Gross Profit based on Category Contribution -", input$country),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
pie_chart
})
}
# Run the Shiny app
shinyApp(ui = ui, server = server)
In this code, the UI has been defined using fluidPage and includes a title panel, a sidebar panel with a dropdown menu for selecting a country, and a main panel that displays a pie chart using plotlyOutput.
The server function has been used to define the server logic, rendering a plotly pie chart based on user-selected data from the CoProfit dataset for the chosen country. The category column is assigned to the labels variable and the GProfit column is assigned to the values variable. An interactive pie chart is created using plot_ly, with textinfo set to show both the label and percentage and insidetextorientation set to ‘radial’.
The layout function is used to customize the pie chart, with the title displaying the selected country name and the x and y axes configured to hide grid lines, zero lines, and tick labels.
Lastly, the shinyApp function is used to run the Shiny app, combining the UI and server components. Users can easily select a country and view an interactive pie chart displaying the total gross profit based on category contribution for that country.